<?php
// +----------------------------------------------------------------------
// | 鸿运集团 [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use \think\Cookie;
use app\admin\controller\Permissions;
/**
 * 客房管理
 */
class House extends Permissions
{
    public function index()
    {
       
       
    }
	
	/**
	 * 房间管理
	 * */
	public function house_list(){
        $post = $this->request->param();
		$start_time = '';
		$end_time = '';
		if(!empty($post['start_time']) && !empty($post['end_time'])){
			$start_time = $post['start_time'];
			$end_time = $post['end_time'];
			$where['a.add_time'] = ['between time',[$post['start_time'],$post['end_time']]];
		} 
        if (isset($post['store_id']) and !empty($post['store_id'])) {
            $where['a.store_id'] = $post['store_id'];
        }
		if (isset($post['room_id']) and !empty($post['room_id'])) {
            $where['a.room_id'] = $post['room_id'];
        }
		if (isset($post['num_id']) and $post['num_id'] > 0) {
            $where['a.num_id'] = ['like', '%' . $post['num_id'] . '%'];
        }
		
        $house = empty($where) ? db('house')->alias('a')->join('store b','a.store_id=b.id','LEFT')->join('room_sort c','a.room_id=c.id')->field('a.*,b.store_name,c.room_name')->order('a.add_time desc')->paginate(20) : db('house')->alias('a')->join('store b','a.store_id=b.id','LEFT')->join('room_sort c','a.room_id=c.id')->field('a.*,b.store_name,c.room_name')->where($where)->order('a.add_time desc')->paginate(20,false,['query'=>$this->request->param()]);
        
        $this->assign('start_time',$start_time);
		$this->assign('end_time',$end_time);
        $info['store'] = Db::name('store')->select();
		$info['room'] = Db::name('room_sort')->select();
        $this->assign('info',$info);
		 $this->assign('house',$house);
		return $this->fetch();
	}
		
	/**
	 * 添加&编辑房间
	 * */
	public function add_house(){
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    	if($id > 0) {
    		//是修改操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
				$post['add_time'] = date('Y-m-d H:i:s',time());
				$res = db('house')->where('id',$id)->update($post);
                if($res==true){
                	return $this->success('修改房间成功','admin/house/house_list');
                }else{
                	return $this->error('修改失败');
                }
    		} else {
    			//非提交操作
    			$info['house'] = db('house')->where('id',$id)->find();
    			$info['hyjt_store'] = Db::name('store')->select();
				$info['room'] = Db::name('room_sort')->where('store_id',$info['house']['store_id'])->field('room_name,id')->select(); 
    			$this->assign('info',$info);
    			$this->assign('id',$id);
    			return $this->fetch();
    		}
    	} else {
    		//是新增操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
	            //验证房间名是否存在
	            $name = db('house')->where('h_name',$post['h_name'])->select();
	            if(!empty($name)) {
	            	return $this->error('提交失败：该房间名称已被占用');
	            }
				$post['add_time'] = date('Y-m-d H:i:s',time());
	            $res = db('house')->insert($post);
                if($res==true){
                	return $this->success('添加房间成功','admin/house/house_list');
                }else{
                	return $this->error('添加房间失败');
                }
    		} else {
    			//非提交操作
    			$info['hyjt_store'] = Db::name('store')->select();
				$info['room'] = Db::name('room_sort')->select();
    			$this->assign('info',$info);
    			return $this->fetch();
    		}
    	}
		return $this->fetch();
	}

 	/**
     * 房间分类删除
     */
    public function house_delete()
    {
    	if($this->request->isAjax()) {
    		$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    		$res = db('house')->where('id',$id)->delete();
    		if($res==true) {
    			return $this->success('删除成功','admin/house/house_list');
    		}else{
                return $this->error('删除失败');
    		}
    	}
    }
	

	
	/**
	 * 房间类型
	 * */
	public function room_type(){
        $post = $this->request->param();
        if (isset($post['store_id']) and !empty($post['store_id'])) {
            $where['a.store_id'] = $post['store_id'];
        }
		if (!empty($post['room_name'])) { 
            $where['a.room_name'] = ['like', '%' . $post['room_name'] . '%'];
        }
		
        $room = empty($where) ? db('room_sort')->alias('a')->join('store b','a.store_id=b.id','LEFT')->field('a.*,b.store_name')->order('a.show_dec desc')->paginate(20) : db('room_sort')->alias('a')->join('store b','a.store_id=b.id','LEFT')->field('a.*,b.store_name')->where($where)->order('a.show_dec desc')->paginate(20,false,['query'=>$this->request->param()]);
        
        $this->assign('room',$room);
        $info['store'] = Db::name('store')->select();
        $this->assign('info',$info);
		return $this->fetch();
	}

	/**
	 * 添加房间分类
	 * */
	public function add_room(){
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    	if($id > 0) {
    		//是修改操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
				$post['add_time'] = date('Y-m-d H:i:s',time());
				$res = db('room_sort')->where('id',$id)->update($post);
                if($res==true){
                	return $this->success('修改房间分类成功','admin/house/room_type');
                }else{
                	return $this->error('修改失败');
                }
    		} else {
    			//非提交操作
    			$info['room'] = db('room_sort')->where('id',$id)->find();
    			$info['hyjt_store'] = Db::name('store')->select();
    			$this->assign('info',$info);
    			return $this->fetch();
    		}
    	} else {
    		//是新增操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
	            //验证房间名是否存在
	            $name = db('room_sort')->where('room_name',$post['room_name'])->select();
	            if(!empty($name)) {
	            	return $this->error('提交失败：该房间分类已被占用');
	            }
				$post['add_time'] = date('Y-m-d H:i:s',time());
	            $res = db('room_sort')->insert($post);
                if($res==true){
                	return $this->success('添加房间分类成功','admin/house/room_type');
                }else{
                	return $this->error('添加房间分类失败');
                }
    		} else {
    			//非提交操作
    			$info['hyjt_store'] = Db::name('store')->select();
    			$this->assign('info',$info);
    			return $this->fetch();
    		}
    	}
		return $this->fetch();
	}

    /**
     * 房间分类删除
     */
    public function room_delete()
    {
    	if($this->request->isAjax()) {
    		$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
    		$res = db('room_sort')->where('id',$id)->delete();
    		if($res==true) {
    			return $this->success('删除成功','admin/house/room_type');
    		}else{
                return $this->error('删除失败');
    		}
    	}
    }
	
	/**
     * 获取门店下的分类
     */
    public function get_store()
	{
		$store_id = input('store_id');
		$room = db('room_sort')->where('store_id',$store_id)->field('room_name,id')->select();
		return json(["data"=>$room]);
	}
}
